<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2020/1/13
 * Time: 13:38
 */

namespace app\api\controller\v1;
use app\api\controller\common\Mail;
use think\Controller;
use app\api\controller\common\ValidateCode;
use think\Cookie;
use think\Db;
use app\api\model\User as UserModel;
use app\api\model\ValidateCode as ValidateCodeModel;
use app\api\controller\common\AccessToken;
class User extends Controller
{
    public function register(){
        $params = input("post.");
        $user = new UserModel;
        $params['status'] = 1;
        $user->save($params,['verify_sign'=>Cookie::get('verify_sign')]);
        //删除之前的用户的临时标识
        Cookie::delete("verify_sign");
        return [
            'code'=>200,
            'msg'=>'设置信息成功,可用返回登录!'
        ];
    }

    public function sendRegisterMail(){
        $email = input("email");//接收收件人邮件地址

        //实现数据验证 和 注册逻辑,然后再发送邮件
        $mail = new Mail();

        //模板渲染为一个内容字符串
        $content = $this->fetch ("template/register_code",["date"=>date("Y-m-d H:i:s",time()),'code'=>ValidateCode::createCode($email)]);

        //准备发送的数据
        $params = [
            'to'=>$email,
            'title'=>'极果网-注册验证码',
            'content'=>$content
        ];
        //开始发送邮件
        $mail->send($params);

        if($mail){
            return [
                'code'=>200,
                '邮件发送成功 ,请登录邮箱查收!'
            ];
        }else{
            return [
                'code'=>0,
                '邮件发送失败,请重试!'
            ];
        }
    }

    public function checkRegister(){//最后的注册确认
        $params = input("post.");
        //获取到cookie的verify_sign标识
        $params['verify_sign'] = Cookie::get("verify_sign");
        $params['status'] = 0;
        $res = Db::table("validate_code")->where($params)->select();
        if($res){//确认邮箱 验证码 标识可用 ,可用开始生成账号,但是属于未激活
            //将用户的记录到用户表中 (users)
            $user = new UserModel([
                'email'=>$res[0]['email']
            ]);
            $user->save();
            //更新验证码的状态, 失效
            $vc = new ValidateCodeModel;
            $vc->where(['id'=>$res[0]['id']])->update(['status'=>1]);

            return [
                'code'=>200,
                'msg'=>'注册成功~',
                'data'=>$res
            ];
        }else{
            return [
                'code'=>0,
                'msg'=>'注册失败~'
            ];
        }
    }

    public function login(){
        $params = input("post.");
        $params['password'] = md5('JG-2020'.md5($params['password']));
        $params['status'] = 1; //登录的条件,必须是为激活状态

        $res= UserModel::get($params);
        if($res){
            $user = $res->getData();
            //生成token ,并记录 ,返回
            $ak = new AccessToken($user['uid']);
            $token = $ak->getToken();
            //记录登录的信息到cookie
            Cookie::set("nick",$res['nick'],30*24*60*60);
            Cookie::set("token",$token,7200);
            return [
                'code'=>200,
                'token'=>$token,
                'nick'=>$res['nick']
            ];
        }else{
            return [
                'code'=>0,
                'msg'=>'登录失败!'
            ];
        }
        //记录登录历史

    }
}